const crypto = require('crypto')
// 微信支付回调函数内容
var str = {
  request: {
    method: 'POST',
    url: '/notify/wxnotify',
    header: {
      host: 'md.jinghuanqiu.com',
      'x-real-ip': '101.226.103.24',
      'x-forwarded-for': '101.226.103.24',
      'remote-host': '101.226.103.24',
      connection: 'close',
      'content-length': '907',
      'user-agent': 'Mozilla/4.0',
      accept: '*/*',
      'wechatpay-nonce': '9cTsdbr4i5fJSZ0ibrHm9ZdvxPyBd1mr',
      'content-type': 'application/json',
      'wechatpay-serial': '354A3CEA0E3C01199B671734EE937C81B2BFB042',
      'wechatpay-signature': 'ZD1nclkGo4u5YTRq+1jA9cq2jmPX6+HF5j9cqzZrx1foXcaNs7OzdRTuAI8wCYTJvENs7SCVwfOdhyvclCQHcFBploXoNFh8tlfdZDSKk7I/iyTJEifl1iCA3zN5J01KOHBAYKmVI+T4G7wmj5YiYwTjjIyUUDQCNeeb9Lu5okrBvEjjHKAtR5NQfWAUQBR+5KC2kkzg82YadWY84i7G9FGRYi9S/LbqvF/15ZQonP6p0/jgmE5lHfZ59Wvy8yhAvSg4cm4QK4vePj+jtpLHe9n+IweVwDzQJu2V3iKPQsBSTZ8ZiWp4cP1stOpLoSzOaIhMTNPNOczuTBmc3teaDA==',
      'wechatpay-signature-type': 'WECHATPAY2-SHA256-RSA2048',
      pragma: 'no-cache',
      'wechatpay-timestamp': '1685975227'
    }
  },
  response: {
    status: 404,
    message: 'Not Found',

  },
  app: { subdomainOffset: 2, proxy: false, env: 'local' },
  originalUrl: '/notify/wxnotify',
  req: '<original node req>',
  res: '<original node res>',
  socket: '<original node socket>'
}

let body = {
  id: 'e0fed300-28e1-504c-80cb-c426f54c01df',
  create_time: '2024-05-07T15:35:29+08:00',
  resource_type: 'encrypt-resource',
  event_type: 'TRANSACTION.SUCCESS',
  summary: '支付成功',
  resource: {
    original_type: 'transaction',
    algorithm: 'AEAD_AES_256_GCM',
    ciphertext: 'l9Cq8cpY4cfmUn+B16IPR4WwrNDDbGy2O15sl2yje+GVkyJ/M81TE2WCxolHCtdh/RUg5IfPVMq6UdBEjdOjjKUISPSlV2E3YGlEP/jnbJu26gwdfWl4WJoSd2hzcJnUPm+XwgSdQgNKxELnVVYaIG604YqJq1h3t9yBnAb6vsj2eZPGar85pMW3MR7Y6nJ0wbWDPZ2hnlfAcG5wKyh6Koy+wwy+HEHX+Q5qSIj9P0AFvb7uQdLQBV8GwdkCj1PvPFHW2e3AVfBQfbkLlRiaFV0B+amj1qVjOOhqr0MjRXZtznZ6zfW+11dJ8VJytUlkC3wlyLUEsARYmKGkAREyztu1QLnCJ+qhJ6UoGNoWvBNi9OQpmqoAtfI5H/Gsv/mwz9f+VoEp0s8whQEkvK2xfgAK0Oe7m4RUejeWyecRqQ0B2VKHpBjPcBhyNgE3eUnyTh014XoYOWk93WizkasWQocExMb8xS4TGCUigEvsjtl9O4dRSteD2va/es7oXPHT44tuh4+tyYaIGch9tsjqmbjndRzve+uf/tIiVvMOq+I5iICXasoeB4ulluSJ63o4ILlx',
    associated_data: 'transaction',
    nonce: 'NTNVrcn65mid'
  }
}
let { ciphertext, nonce, associated_data } = body.resource

let pay_api_key = 'dhjgsfGVGfesdecShGHDHDHVHJJdeewe'
console.log(veryV3(ciphertext, pay_api_key, nonce, associated_data));


function veryV3(cipherText, key, iv, add) {
  cipherText = Buffer.from(cipherText, 'base64');
  let authTag = cipherText.slice(cipherText.length - 16);
  let data = cipherText.slice(0, cipherText.length - 16);
  let decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
  decipher.setAuthTag(authTag);
  decipher.setAAD(Buffer.from(add));
  let decoded = decipher.update(data, null, 'utf8');
  decipher.final();
  return JSON.parse(decoded);
}

